//
//  MoreView.m
//  BigJoy
//
//  Created by Rowling on 2017/7/13.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "MoreView.h"
#import "MoreViewCell.h"

@interface MoreView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIView * contentView;
@property (nonatomic,strong) UITableView *talbeView;
@end
@implementation MoreView

-(UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc]init];
    }
    return _contentView;
}

-(UITableView *)talbeView {
    if (_talbeView == nil) {
        _talbeView = [[UITableView alloc]init];
        _talbeView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _talbeView.backgroundColor = [UIColor clearColor];
    }
    return _talbeView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

-(void)setUI {
    self.backgroundColor = [UIColor clearColor];
    self.contentView.frame = self.bounds;
    self.contentView.backgroundColor = [UIColor clearColor];
    UIButton * btn = [UIButton new];
    [btn setImage:[UIImage imageNamed:@"refresh_arrow"] forState:UIControlStateNormal];
    @weakify(self);
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismis];
    }];
    [btn sizeToFit];
    [self.contentView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(32);
        make.left.equalTo(self.contentView).offset(15);
    }];
    [self.contentView addSubview:self.talbeView];
    self.talbeView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
    self.talbeView.delegate = self;
    self.talbeView.dataSource = self;
    [self addSubview:self.contentView];
    [self addbur];
}

-(void)addbur {
    // 加入毛玻璃效果
    UIBlurEffect * blurE = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView * blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurE];
    blurEffectView.frame = [UIScreen mainScreen].bounds;
    blurEffectView.alpha = 0.95;
    [self.contentView insertSubview:blurEffectView atIndex:0];
}

-(void)animationAddToView:(UIView *)view {
    [view addSubview:self];
    [self doAnimation];
}

-(void)dismis {
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.x = SCREEN_WIDTH;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)doAnimation {
    self.contentView.x = SCREEN_WIDTH;
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.x = 0;
    }];
}

#pragma -mark: delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MoreViewCell * cell = [MoreViewCell cellWithTalbelView:tableView];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 84.5;
}

-(void)dealloc {
    NSLog(@"view dealloc");
}

@end
